package cn.imu.config;

import java.util.HashMap;
import java.util.Map;

public class ServiceConfig {
    //启动服务时，需要将配置文件的信息读取放到serviceMap中
    public static HashMap serviceMap=new HashMap();

    public static String[] getService(String serviceName){
        serviceMap=(HashMap)prepareServiceMap();
        String[] service=new String[2];
        String serviceClass="";
        /*
        serviceClass=ServiceConfig.serviceMap.get(serviceName).toString();
        会出现错误,后面带有哈希值MoAttack{geLi=cn.imu.impl.LiuDeHua@5a63f509}
         */
        serviceClass=ServiceConfig.serviceMap.get(serviceName).toString();
        //将截取前的数据放入到数组中
        service[0]=serviceClass;
        //System.out.println(serviceClass);
        //需要截取字符串
        if(serviceClass.contains("@")){
            //返回“@”第一次出现的位置
            int i = serviceClass.indexOf("@");
            //MoAttack{geLi=cn.imu.impl.LiuDeHua@5a63f509}这种情况，容易截去后面括号,所以需要添加判断
            if(serviceClass.contains("{")){
                serviceClass=serviceClass.substring(0,i)+"}";
            }else{
            serviceClass=serviceClass.substring(0,i);
            }
            //截取后的字符串放入到数组中
            service[1]=serviceClass;
        }
        //System.out.println(serviceClass);
        return service;
    }



    private static Map prepareServiceMap(){
        Map map=null;
        BeanFactory factory=new BeanFactory();
        try {
            map = factory.init("bean2.xml");
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return map;
    }
}
